﻿package com.game.view.component.icon
{
	import com.dusk.util.BitmapUtil;
	import com.dusk.util.ResUtil;
	import com.game.data.vo.EquipmentVO;
	import com.game.manager.GameSceneManager;
	import com.game.module.backpack.view.PackThings;
	import com.game.view.component.ComponentBase;
	import com.game.view.component.notification.AttributeBox;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	public class RoleEquipIcon extends ComponentBase
	{
		
		public function RoleEquipIcon(param1:EquipmentVO)
		{
			super();
			this.name = "curzb";
			this.myEquipObj = param1;
			this.buttonMode = true;
		}
		
		private var _icon:Bitmap = null;
		private var myEquipObj:EquipmentVO;
		private var attribBox:AttributeBox;
		
		public function activate():void
		{
			this.addEventListener(MouseEvent.ROLL_OVER, this.showAttribute);
			this.addEventListener(MouseEvent.ROLL_OUT, this.removeAttribute);
			this.addEventListener(MouseEvent.MOUSE_MOVE, this.refreshPoint);
		}
		
		public function deactivate():void
		{
			this.removeEventListener(MouseEvent.ROLL_OVER, this.showAttribute);
			this.removeEventListener(MouseEvent.ROLL_OUT, this.removeAttribute);
			this.removeEventListener(MouseEvent.MOUSE_MOVE, this.refreshPoint);
		}
		
		public function removeAttribCon():void
		{
			if (this.attribBox)
			{
				this.attribBox.removeFromParent();
				this.attribBox = null;
			}
		}
		
		public function getEquipVO():EquipmentVO
		{
			return this.myEquipObj as EquipmentVO;
		}
		
		override protected function onAdded():void
		{
			if (_icon == null)
			{
				_icon = ResUtil.getImageBitmap(myEquipObj.equipId);
				if (!_icon)
				{
					_icon = BitmapUtil.createPureBitmap();
				}
			}
			if (!contains(_icon))
			{
				addChild(_icon);
			}
			activate();
		}
		
		override protected function onRemoved():void
		{
			if (this._icon != null)
			{
				(this._icon as Bitmap).bitmapData.dispose();
				this._icon = null;
			}
			this.removeAttribCon();
			this.deactivate();
		}
		
		private function showAttribute(param1:MouseEvent):void
		{
			this.attribBox = new AttributeBox(this.myEquipObj);
			var mousePoint:Point = new Point(GameSceneManager.getIns().mainStage.mouseX, GameSceneManager.getIns().mainStage.mouseY);
			this.attribBox.x = mousePoint.x + 10 + this.attribBox.width > 960 ? mousePoint.x - 10 - this.attribBox.width : mousePoint.x + 10;
			this.attribBox.y = 590 - this.attribBox.height > mousePoint.y ? mousePoint.y : 590 - this.attribBox.height;
			GameSceneManager.getIns().addScene(this.attribBox);
			if (this.parent is PackThings)
			{
				PackThings(this.parent).setTxtVisible(false);
			}
		}
		
		private function removeAttribute(param1:Event):void
		{
			this.removeAttribCon();
			if (this.parent is PackThings)
			{
				PackThings(this.parent).setTxtVisible(true);
			}
		}
		
		private function refreshPoint(param1:MouseEvent):void
		{
			var mousePoint:Point = null;
			if (this.attribBox)
			{
				mousePoint = new Point(GameSceneManager.getIns().mainStage.mouseX, GameSceneManager.getIns().mainStage.mouseY);
				this.attribBox.x = mousePoint.x + 10 + this.attribBox.width > 960 ? mousePoint.x - 10 - this.attribBox.width : mousePoint.x + 10;
				this.attribBox.y = 590 - this.attribBox.height > mousePoint.y ? mousePoint.y : 590 - this.attribBox.height;
			}
		}
	}
}
